<?php
namespace BoxSns\Logic;
class MemberLogic extends \HomeController {
	public function index() {
		echo "mall Logic";
	}

	/*******去登录*****/
	public function toLogin() {
		$this->store_id = getStoreID();
		$backUl = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
		$backUl = urlencode($backUl);
		header("Location:" . LoginUrl . "/Login/index/store_id/{$this->store_id}/?backUrl={$backUl}");
	}

	/*******登录*****/
	public function saveLogin($user_id) {
		/****登录其它地方的用户先下线****/
		if (C('LOGIN_ONE')) {
			$this->dologout($user_id);
		}
		/********保存登录****/
		session("user_id", $user_id);
	}

	/*******根据user_auth_sign查找用户信息*****/
	public function auth_info($code) {
		$data = S($code);
		return $data;
	}

	/*********退出登录**/
	public function dologout($user_id) {
		$class = C('SESSION_OPTIONS');
		$class = '\\' . $class['type'];
		$session_drive = new $class();
		$r = $session_drive->destroy_by($user_id);
		$this->clear_sign();
		setcookie("AuthSign", "", -time() + 3600 * 24 * 14, "/", "." . DomainName);
		setcookie("AuthSign", "", -time() + 3600 * 24 * 14, "/");
		setcookie("UserCookieData", "", -time() + 3600 * 24 * 14, "/", "." . DomainName);
		setcookie("UserCookieData", "", -time() + 3600 * 24 * 14, "/");
	}

	/***重置$sign**/
	public function clear_sign() {
		ignore_user_abort(true);
		$AuthSign = json_decode(session("AuthSign"), true);
		if (!$AuthSign) {
			$AuthSign = json_decode($_COOKIE['AuthSign'], true);
		}
		$sign = $AuthSign['sign'];
		S($sign, NULL);
	}

	/*********单点退出登录**/
	public function logout() {
		session_destroy();
		$this->dologout($this->user_id);
		return $this->return_array(0, "");
	}

	//***********保存cookie登录***/
	public function set_login() {
		if ($this->user_id) {
			$user_info = BoxModel("system_member")->where(array('user_id' => $this->user_id))->find();
			$data = array(
				'user_name' => $user_info['user_name'],
				'password' => $user_info['password'],
				'user_id' => $user_info['user_id'],
			);
			$UserCookieData = json_encode($data);
			setcookie("UserCookieData", $UserCookieData, time() + 3600 * 24 * 100, "/");
		} else {
			$data = json_decode($_COOKIE['UserCookieData'], true);
			$user_info = BoxModel("system_member")->where($data)->find();
			if (is_array($user_info)) {
				$this->saveLogin($user_info['user_id']);
				$this->do_login($user_info['user_name'], $user_info['password']);
				$this->user_id = $user_info['user_id'];
			}
		}
		return $user_info;
	}

	/*******单点登录*****/
	public function do_login($user_name, $password, $remember = 0, $backUrl) {
		$user_info = BoxModel("SystemMember")->ByField("user_name", $user_name);
		if (!$user_info) {
			return $this->return_array(1, "帐号不存在");
		}
		if ($user_info['password'] != $password) {
			return $this->return_array(1, "密码不正确");
		}

		/**********生成key***************/
		$data = array(
			'user_id' => $user_info['user_id'],
			'password' => $user_info['password'],
			'last_login_time' => time(),
			'user_name' => $user_info['user_name'],
			'email' => $user_info['email'],
			'phone' => $user_info['phone'],
		);
		ksort($data);
		$sign = dataAuthSign(json_encode($data));
		$data['sign'] = $sign;
		S($sign, $data, 600); //登录信息十分钟内有效的缓存起来
		session("AuthSign", json_encode($data)); //登录了
		/***********保存cookie登录***/
		if ($remember) {
			$AuthSign = json_encode($data);
			setcookie("AuthSign", $AuthSign, time() + 3600 * 24 * 14, "/");
		}
		/********登录成功之后跳转******/
		$bool = strpos($backUrl, "?");
		$backUrl = $bool === false ? ($backUrl . "?box_login_code=" . $sign) : ($backUrl . "&box_login_code=" . $sign);
		return $this->return_array("0", $backUrl);
	}

	/*******注册帐号**/
	public function check_user_name($parms) {
		$user_name = $parms['user_name'];
		$fields = array('phone', 'email', 'user_name');
		foreach ($fields as $field) {
			$r = BoxModel("SystemMember")->ByField($field, $user_name);
			if ($r) {
				$data[$field] = $r;
			}

		}
		if (!empty($data)) {
			return $this->return_array("0", $data);
		} else {
			return $this->return_array("1", "用户不存");
		}
	}

	/*****用户是否正确*****/
	public function check($user_id) {
		if (!intval($user_id)) {
			return array(
				'error' => 1,
				'msg' => '用户ID不能为空',
			);
		}
		$data = BoxModel("SystemMember")->load($user_id);
		if (!$data) {
			return array(
				'error' => 2,
				'msg' => '用信息不存在',
			);
		}

	}

	/******绑定微信用户****/
	public function WxCreateMember($data) {
		$row = array(
			'unionid' => $data['unionid'],
			'user_name' => $data['openid'],
			'nickname' => $data['nickname'],
			'sex' => $data['sex'],
			'area' => $data['city'],
			'address' => $data['province'] . $data['city'],
			'headimg' => $data['headimgurl'],
			'openid' => $data['openid'],
			'store_id' => 1,
			'channel_id' => $data['keyName'],
			'openid' => $data['openid'],
		);
		$model = BoxModel("SystemMember");
		$openid = $data['unionid'] ? $data['unionid'] : $data['openid'];
		$where = array(
			'type_uid' => $openid,
			'type' => 'weixin',
		);
		$sync_login = BoxModel("system_sync_login");
		$sync_info = $sync_login->where($where)->find();
		if (!$sync_info) {
			$user_id = $model->create_member($row);
		} else {
			$row['user_id'] = $sync_info['uid'];
			$user_id = $model->create_member($row);
		}
		/***********保存成第三方登陆*****/
		$where['uid'] = $user_id;
		if (!$sync_info) {
			$sync_login->add($where);
		}
		return $user_id;
	}

	/*********用户充值*****/
	public function recharge($data) {
		if (!is_numeric($data['money'])) {
			return $this->return_array("1", "充值金额不合法");
		}
		$this->order_amount = $data['money'];
		$modelPay = BoxModel("system_money_order");
		$modelPay->startTrans(); //开启事务
		$order_sn = createOrderNo(); //子订单号
		/******订单总表****/
		//$HomeUrl = BOX_MODULE_NAME == "Wap" ? MEduUrl : EduUrl;
		$suceess_url = HomeUrl . U('User/money');
		$row = array(
			'store_id' => 1,
			//'order_sn' => $order_sn,
			'order_type' => 'recharge',
			'addtime' => date("Y-m-d H:i:s"),
			'user_id' => $this->user_id,
			'payment_id' => $data['payment_id'],
			'order_amount' => $this->order_amount,
			'suceess_url' => $suceess_url,
			'title' => "余额充值,订单号：" . $order_sn,
			// 'type' => 'recharge',
			'body' => "余额充值,订单号：" . $order_sn,
		);
		$r = Logic("BoxMall", "Order")->add($row);
		$order_sn = $r['order_sn'];
		if (!$r['order_sn']) {
			$modelPay->rollback();
		} else {
			$modelPay->commit();
			return $this->return_array(0, HomeUrl . "/BoxPay/Order/index/order_sn/" . $order_sn . ".html?openid=" . $this->openid);
		}

	}

	/**********充值成功****/
	public function recharge_sucess($order_sn) {
		if (!session("pay_sucess")) {
			return false;
		}
		session("pay_sucess", null);
		$data = BoxModel("system_money_order")->load($order_sn);
		/*******修改订单状态*****/
		$r = BoxModel("system_money_order")->where("order_sn='$order_sn'")->save(array('status' => 2, 'pay_time' => date("Y-m-d H:i:s")));
		/******购买课程获得突击币***/
		$num = $data['order_amount'];
		$row = array(
			'user_id' => $data['user_id'],
			'num' => $num,
			'remark' => "在线充值",
			'order_sn' => $order_sn,
			'type' => "money",
		);
		//dump($row);
		$r = Logic('BoxSNS', 'Log')->add($row);
		/*************金额支出*****/
		$num = $data['order_amount'];
		$row = array(
			'user_id' => $data['user_id'],
			'num' => $num,
			'remark' => "在线充值：" . $data['order_amount'] . "元",
			'order_sn' => $order_sn,
			'type' => "online_pay",
		);
		$r = Logic('BoxSNS', 'Log')->add($row);
		/*******加到财务统计******/
		$row['type'] = "recharge";
		hook("Finance/Index/add", $row);
		return $r;
	}

	/********用户的发帖数、粉丝数等**/
	public function statistics($param) {
		$user_id = $param['user_id'];
		$store_id = $param['store_id'];
		$key = "user_statistics_" . $user_id;
		if ($user = S($key)) {
			return $user;
		}
		$user['topic_count'] = BoxModel("sns_topic")->where("user_id='$user_id' and is_show=1 and store_id='$store_id'")->count();
		$user['fans_count'] = BoxModel("sns_friends")->where("to_user_id='$user_id'")->count();
		$user['follow_count'] = BoxModel("sns_friends")->where("user_id='$user_id'")->count();
		S($key, $user);
		return $user;
	}

	/*******用户信息*****/
	public function info($param) {
		$user_id = $param['user_id'];
		$r = $this->check($user_id);
		if ($r['error'] > 0) {
			return $r;
		}
		$data = BoxModel("system_member")->load($user_id);
		return $data;
	}

	/*******保存用户信息*****/
	public function save($param) {
		$param['user_id'] = $this->user_id;
		BoxModel("system_member")->create_member($param);
		return array(
			'error' => 0,
			'msg' => $data,
		);
	}

}
